package com.alan.repository.impl;

import com.alan.entity.teach;
import com.alan.entity.teacherstudent;
import com.alan.repository.TeacherRepository;
import com.alan.utils.JDBCTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TeacherRepositoryImpl implements TeacherRepository {
    @Override
    public teach login(String username, String password) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        teach teach = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from teacher where name=? and password=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                teach = new teach(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return teach;
    }

    @Override
    public String registerfind(String username) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String re = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from teacher where name=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, username);
            resultSet = preparedStatement.executeQuery();

            if (resultSet.next()) {
                re = "success";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return re;
    }


    @Override
    public List<teacherstudent> findall(String stu_class, int index, int limit) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select * from student where stu_class=? limit ?,?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<teacherstudent> list = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, stu_class);
            preparedStatement.setInt(2, index);
            preparedStatement.setInt(3, limit);
            resultSet = preparedStatement.executeQuery();
            while ((resultSet.next())) {
                teacherstudent teacherstudent = new teacherstudent(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3), resultSet.getString(4), resultSet.getString(5), resultSet.getInt(6), resultSet.getDouble(7), resultSet.getDouble(8), resultSet.getString(9));
                list.add(teacherstudent);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int counts(String stu_class) {
        Connection connection = JDBCTools.getConnection();
        String sql = "select count(*) from student where stu_class=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<teacherstudent> list = new ArrayList<>();
        int counts = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, stu_class);
            resultSet = preparedStatement.executeQuery();

            while ((resultSet.next())) {
                counts = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return counts;

    }

    @Override
    public void update(String stu_id, String Cname, String Ename, String Ntel, String Nclass, int semester) {
        Connection connection = JDBCTools.getConnection();
        String sql = "update  student set chinese_name=?,english_name=?,tel=?,stu_class=?,semester=? where student_id=? ";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, Cname);
            preparedStatement.setString(2, Ename);
            preparedStatement.setString(3, Ntel);
            preparedStatement.setString(4, Nclass);
            preparedStatement.setInt(5, semester);
            preparedStatement.setString(6, stu_id);
            int a = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(String name) {

        Connection connection = JDBCTools.getConnection();
        String sql = "update  student set stu_status=? where student_id=? ";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, "L");
            preparedStatement.setString(2, name);
            int a = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void change(String name, String password) {
        Connection connection = JDBCTools.getConnection();
        String sql = "update  teacher set password=? where name=? ";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setString(2, name);
            int a = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void add(String name, String password, String t_class) {
        Connection connection = JDBCTools.getConnection();
        String sql = "insert into teacher values (?,?,?) ";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, password);
            preparedStatement.setString(3, t_class);
            int a = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public teach find(String name, String t_class) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        teach teach = null;
        try {
            connection = JDBCTools.getConnection();
            String sql = "select * from teacher where name=? and t_class=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setString(2, t_class);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                teach = new teach(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(connection, preparedStatement, resultSet);
        }
        return teach;
    }

    @Override
    public void newpassword(String name, String t_class, String password) {
        Connection connection = JDBCTools.getConnection();
        String sql = "update teacher set password=?  where name=? and t_class=?";
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, password);
            preparedStatement.setString(2, name);
            preparedStatement.setString(3, t_class);
            int a = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
